﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Subtle.ViewModels;
using Subtle.Domain.Services.Interfaces;
using Subtle.Domain.Models;
using Subtle.Domain.Services;
using System.Security.Principal;

namespace Subtle.Controllers
{
	public class AccountController : BaseController
	{
		private IUserService userService;

		/// <summary>
		/// Initializes a new Account Controller
		/// </summary>
		/// <param name="userService"></param>
		/// <param name="notificationService"></param>
		public AccountController(IUserService userService)
		{
			this.userService = userService;
		}

		/// <summary>
		/// Register User
		/// </summary>
		/// <returns></returns>
		public ActionResult Register()
		{
			return View();
		}

		/// <summary>
		/// Register User
		/// </summary>
		/// <param name="accountRegisterViewModel"></param>
		/// <returns></returns>
		[HttpPost]
		public ActionResult Register(AccountRegisterUserViewModel accountRegisterViewModel)
		{
			User user = userService.GetUser(accountRegisterViewModel.Username);

			if (ModelState.IsValid)
			{
				//The User Exists
				User foundUser = userService.GetUser(user.Username);
				if (foundUser != null)
				{
					ModelState.AddModelError("userAlreadyExists", "A User with the specified email address already exists.");
					return PartialView(accountRegisterViewModel);
				}

				//The User Doesn't Exist and will be created
				if (userService.CreateNonValidatedUser(user) == UserCreateStatus.Success)
				{
					if (userService.SendValidateUserRequest(user))
					{
						return RedirectToAction("Index", "Home");
					}
				}
				else
				{
					ModelState.AddModelError("userWasNotCreated", "Your account could not be created, please try again later.");
					return PartialView(accountRegisterViewModel);
				}
			}
			return View(accountRegisterViewModel);
		}

		/// <summary>
		/// Forgot Your Password
		/// </summary>
		/// <returns></returns>
		public ActionResult ForgotYourPassword()
		{
			return View();
		}

		/// <summary>
		/// Register User
		/// </summary>
		/// <param name="accountForgotYourPasswordViewModel"></param>
		/// <returns></returns>
		[HttpPost]
		public ActionResult ForgotYourPassword(AccountForgotYourPasswordViewModel accountForgotYourPasswordViewModel)
		{
			if (ModelState.IsValid)
			{
				User foundUser = userService.GetUser(accountForgotYourPasswordViewModel.Username);
				if (foundUser == null)
				{
					ModelState.AddModelError("userDoesntExist", "The user does not exist.");
					return Register();
				}
				if (foundUser != null && userService.SendPasswordReminder(foundUser))
				{
					//ToDo
				}
			}
			return View(accountForgotYourPasswordViewModel);
		}

		/// <summary>
		/// Login
		/// </summary>
		/// <returns></returns>
		public ActionResult Login()
		{
			return View();
		}

		/// <summary>
		/// Login View Model
		/// </summary>
		/// <param name="accountLoginViewModel"></param>
		/// <returns></returns>
		[HttpPost]
		public ActionResult Login(AccountLoginViewModel accountLoginViewModel)
		{
			User user = accountLoginViewModel.GetUser();
			if (ModelState.IsValid)
			{
				User foundUser = userService.GetUser(user.Username);
				if (foundUser != null && String.Compare(foundUser.Password, userService.HashPassword(user.Password)) == 0)
				{
					//Make sure that the user's account has been verified
					if (foundUser.Verified)
					{
						SaveAuthentication(foundUser.Username);
						return RedirectToAction("Index", "Home");
					}
					else
					{
						ModelState.AddModelError("userExistsButNotVerified", String.Format("The account has not been verified.", user.Username));
					}
				}
			}
			return View(accountLoginViewModel);
		}

		/// <summary>
		/// Logout View Model
		/// </summary>
		/// <returns></returns>
		public ActionResult Logout()
		{
			FormsAuthentication.SignOut();
			return RedirectToAction("Index", "Home");
		}

		/// <summary>
		/// Activate a User's Account
		/// </summary>
		/// <param name="username"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult Activate(string username, string id)
		{
			//Make sure we have a user that matches the username
			User user = userService.GetUser(username);
			if (user != null)
			{
				if (userService.ValidateUser(user, id))
				{
					return RedirectToAction("Index", "Home");
				}
				else
				{
					ModelState.AddModelError("userActivationError", String.Format("The user {0} could not be activated, the validation code did not match", username));
				}
			}
			return RedirectToAction("Register", "Account");
		}
	}
}
